package com.han.fu.common.utils;

import java.security.SecureRandom;
import java.util.UUID;

/**
 * UUID工具类
 *
 * @author: star
 * @date: 2020-08-26
 */
public class UuidUtil {

    private static final int ZERO = 0;

    private static final int THIRTY_TWO = 32;

    private static final char[] CODE_ARRAY = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
            'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N',
            'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};

    /**
     * 随机获取英文+数字
     *
     * @param num 生成数量
     * @return String
     */
    public static String verifyUserName(int num) {
        // 声明一个StringBuffer存储随机数
        StringBuilder sb = new StringBuilder();
        SecureRandom random = new SecureRandom();
        // 获取英文或数字
        for (int i = ZERO; i < num; i++) {
            char data = CODE_ARRAY[random.nextInt(CODE_ARRAY.length)];
            sb.append(data);
        }
        return sb.toString();
    }

    /**
     * 生成uuid
     *
     * @return uuid
     */
    public static String generateUuid() {
        // 生成一个uuid
        StringBuilder sb = new StringBuilder();
        String preUuid = UUID.randomUUID().toString();
        // 去除-
        sb.append(preUuid.replace("-", ""));
        // 补齐
        sb.append(verifyUserName(THIRTY_TWO - sb.length()));
        return sb.toString();
    }
}
